package xman.mssh.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    public static final String DATE_FORMAT = "yyyy-MM-dd";

    public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static final String MONTH_FORMAT = "yyyy-MM";

    public static final long MONTH_LONG = 2651224907l;

    public static String getDate(int beforeDays) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DAY_OF_MONTH, beforeDays);
        String a = dateToString(c.getTime(), DATE_FORMAT);
        return a;
    }

    public static String getDate(int beforeDays, String dateFormat) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DAY_OF_MONTH, beforeDays);
        String a = dateToString(c.getTime(), dateFormat);
        return a;
    }

    public static String dateToString(Date dateValue) {
        return dateToString(dateValue, DATETIME_FORMAT);
    }

    public static String dateToString(Date dateValue, String format) {
        if (dateValue == null || format == null) {
            return null;
        } else {
            SimpleDateFormat dateFormat = new SimpleDateFormat(format);
            return dateFormat.format(dateValue);
        }
    }

    public static Date stringToDate(String stringValue) {
        return stringToDate(stringValue, DATE_FORMAT);
    }

    public static Date stringToDate(String stringValue, String format) {
        Date dateValue = null;
        if (stringValue != null && format != null) {
            try {
                SimpleDateFormat dateFormat = new SimpleDateFormat(format);
                dateValue = dateFormat.parse(stringValue);
            } catch (ParseException ex) {
                dateValue = null;
            }
        }
        return dateValue;
    }

    public static String getNowYear() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        return String.valueOf(year);
    }

    public static String getNowMonth() {
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH) + 1;
        if (month < 10) {
            return "0" + month;
        } else {
            return String.valueOf(month);
        }
    }

    public static String getNowDay() {
        return dateToString(new Date(), "dd");

    }

    public static String getYestday() {
        Calendar date = Calendar.getInstance();
        date.add(Calendar.DATE, -1);
        return dateToString(date.getTime(), "dd");

    }

    public static Date getDateFront(int monthCount) {
        return new Date(Calendar.getInstance().getTimeInMillis() - MONTH_LONG * monthCount);
    }

    public static String getNowHour() {
        return dateToString(new Date(), "HH");
    }

    public static String getNowMinute() {
        return dateToString(new Date(), "mm");
    }

    public static Date setDate(String stringDate, int num) {
        if (stringDate != null) {
            Date date = stringToDate(stringDate, "yyyy-MM-dd");
            return setDate(date, num);
        } else {
            return null;
        }
    }

    public static Date setDate(Date date, int num) {
        Date dateValue = null;
        Calendar c = null;
        if (date != null) {
            c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.DAY_OF_MONTH, num);
            dateValue = c.getTime();
        }
        return dateValue;
    }

    public static Date addDay(Date myDate, int amount) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(myDate);
        cal.add(Calendar.DAY_OF_MONTH, amount);
        return cal.getTime();
    }
}
